import logging

import pymysql



from common.config import host
from tools.doLog import DoLog

logger=DoLog().getLogger(__name__,logging.INFO)
class DoDb():
    def __init__(self):
        try:
        # 数据库连接对象
            db=pymysql.Connection(user="root",
                                  password="123456",
                                  host=host,
                                  port=3306,
                                  database='crm9')
            # 获取一个处理数据/执行sql语句的对象
            self.runner=db.cursor()
        except Exception as e:
             print(f"发生了异常异常的信息是{e}")
             logger.error("连接失败")
        else:
            logger.info("连接成功")
        finally:
            logger.info("连接成功")


    def domysq(self,sql):
        try:
            s=self.runner.execute(sql)
            # 获取数据
            # 获取所有:fetchall
            # #获取一个:fetchone
            # #获取部分:fetchmany
        except Exception as e:
            print(f"错误语句是{sql}发生了异常异常的信息是{e}")
        else:
            a=self.runner.fetchmany(10)
            logger.info("连接成功")
            print(len(a),type(a),a,sep="\n")



        # print(s)
        return  a
if __name__ == '__main__':
    a=DoDb()
    c=a.domysq(3)
    for i in c:
        print(i[0])


